package com.mondo.financialwallet.presentation.ui.widget;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.v7.widget.RecyclerView;
import android.view.View;


/**
 * 产品时间线
 */
public class TimelineItemDecoration extends RecyclerView.ItemDecoration {

    private int mNodeRadius = 10, mLeftPadding = 100;

    private Paint mNodePaint, mLinePaint;

    public TimelineItemDecoration() {
        // 节点
        mNodePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mNodePaint.setColor(Color.parseColor("#3F82DD"));
        mNodePaint.setStyle(Paint.Style.FILL);

        // 时间线
        mLinePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mLinePaint.setColor(Color.parseColor("#CCCCCC"));
        mLinePaint.setStyle(Paint.Style.STROKE);
        mLinePaint.setPathEffect(new DashPathEffect(new float[]{10, 10}, 0));
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        outRect.set(mLeftPadding, 0, 0, 0);
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDraw(c, parent, state);
        int childCount = parent.getChildCount();

        for (int i = 0; i < childCount; i++) {
            final View child = parent.getChildAt(i);

            // 时间线
            if (i < childCount - 1) {
                c.drawLine(
                        mLeftPadding / 2,
                        child.getTop() + (mNodeRadius * 2) + child.getPaddingTop(),
                        mLeftPadding / 2 + 1,
                        child.getBottom(),
                        mLinePaint
                );
            }

            // 时间节点
            c.drawCircle(
                    mLeftPadding / 2,
                    child.getTop() + mNodeRadius + child.getPaddingTop(),
                    mNodeRadius,
                    mNodePaint
            );
        }
    }

}
